// pages/goods_detail/goods_detail.js
import {request} from '../../request/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObj:{}
  },
  goods:{},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {goods_id} = options;
    this.getGoodsDetail(goods_id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  async getGoodsDetail(a){
    let value = await request({url:'/goods/detail',data:{goods_id:a}});
    this.goods = value.data.message;
    this.setData({
      goodsObj:value.data.message
    })
  },
  previewImg(e){
    const index = e.currentTarget.dataset.index;
    const urls=this.goods.pics.map((a)=>a.pics_mid)
    wx.previewImage({
      urls: urls,
      current:urls[index],
      success: (res) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  handle(){
    let cart = wx.getStorageSync('cart')||[];
    let index = cart.findIndex(v=>v.goods_id===this.goods.goods_id);
    if(index===-1){
      this.goods.num = 1;
      this.goods.check=true
      cart.push(this.goods)
    }else{
      cart[index].num++;
    }
    wx.setStorageSync('cart', cart);
    wx.showToast({
      title: '加入成功',
      icon:'success',
      mask:true
    })
  }
})